/**
 * 
 */
package com.battcn.framework.listener;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.battcn.platform.entity.crow.ProxyIp;
import com.battcn.platform.service.webmagic.CrawlerServeice;
import com.example.rabbitmqTest.Receipt;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;  
/**
 * @author yanglei
 * 2018年1月15日
 */
@Component  
public class RabbitMQListener {  
  
    private final static Logger log= LoggerFactory.getLogger(RabbitMQListener.class);  
  
    @Autowired  
    private ObjectMapper objectMapper;  
    @Autowired
    CrawlerServeice crawlerServeice;
    /*@RabbitListener(queues = "${register.queue.name}",containerFactory = "singleListenerContainer") 
    public void test(@Payload User user){ 
        try { 
            log.debug("消费者监听消费到消息： {} ",user); 
 
        }catch (Exception e){ 
            log.error("消息体解析 发生异常； ",e.fillInStackTrace()); 
        } 
    }*/  
  
    //直接消费模式  
    @RabbitListener(queues = "${register.queue.name}",containerFactory = "singleListenerContainer")  
    public void consumeMessage(Message message, Channel channel){  
    	
        try {  
            long deliveryTag=message.getMessageProperties().getDeliveryTag();  
            byte[] body=message.getBody();  
            List<ProxyIp> entity=objectMapper.readValue(body, List.class);  
            log.debug("消息监听-消费者 接下来进行电影爬虫：{} ",entity);  
            /* crawlerServeice.getMovies();*/
            channel.basicAck(deliveryTag,false); 
        }catch (Exception e){  
            log.error("消息体解析 发生异常； ",e.fillInStackTrace());  
        }  
    }  
    
    //直接消费模式  
    @RabbitListener(queues = "${register.queue.name}",containerFactory = "singleListenerContainer")  
    public void consumeMessage2(Message message, Channel channel){  
    	
        try {  
            long deliveryTag=message.getMessageProperties().getDeliveryTag();  
            byte[] body=message.getBody();  
            List<ProxyIp> entity=objectMapper.readValue(body, List.class);  
            log.debug("消息监听2-消费者 接下来进行电影爬虫：{} ",entity);  
            crawlerServeice.getMovies();
            channel.basicAck(deliveryTag,false); 
        }catch (Exception e){  
            log.error("消息体解析 发生异常； ",e.fillInStackTrace());  
        }  
    }  
  
}  
